#!/bin/sh

#Copyright (c) 2014 Luigi Tarenga <luigi.tarenga@gmail.com>
#Distributed under the terms of a MIT license.

# input:  holder lock_name term value
# return: success holder term value

holder=$1
lname=$2
term=$3
value=$4
success=false

if [ -f state/${lname} ] ; then
   read s_holder s_term s_value < state/${lname}
else
   s_term=0
   s_value=0
fi

if [ "$term" -gt "$s_term" -a "$value" -ge "$s_value" ] ; then
   #give client a vote
   s_holder=$holder
   s_term=$term
   s_value=$value
   success=true
   echo $s_holder $s_term $s_value > state/${lname}
fi

echo $success $s_holder $s_term $s_value
